home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-07-25 | 3.6 KB | 157 lines |
- /*
- * MiniString.java - A string displayed using a mini font. Numbers only
- * Copyright (C) 2000 Romain Guy
- * guy.romain@bigfoot.com
- * www.jext.org
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
- import waba.fx.*;
- import waba.sys.*;
-
- /**
- * This class is used to display a string made of numbers and
- * whitespaces using a tiny font.
- * @author Romain Guy <guy.romain@bigfoot.com>
- * @version 1.0
- */
-
- public class MiniString
- {
- // contains the images of the chars
- public static Image[] FONT_CHARS = new Image[16];
- // font constants
- /** Font width in pixels. **/
- public static final int FONT_WIDTH = 5;
- /** Font height in pixels. **/
- public static final int FONT_HEIGHT = 5;
-
- // mini string modes
- /** Horizontal display. **/
- public static final byte HORIZONTAL = 0;
- /** Vertical display. **/
- public static final byte VERTICAL = 1;
-
- // the text to be displayed
- private byte[] datas;
- private int length;
- // mode
- private byte mode;
-
- /**
- * Creates a new mini string.
- * @param text The text to be displayed
- */
-
- public MiniString(byte[] datas, byte mode)
- {
- this.datas = datas;
- this.mode = mode;
- this.length = this.datas.length;
- }
-
- /**
- * Free the resources occupied by the font.
- */
-
- public static void free()
- {
- for (int i = 0; i < FONT_CHARS.length; i++)
- FONT_CHARS[i].free();
- }
-
- // returns the requested image. if it does not exist,
- // the image is created and then stored for a future
- // use.
-
- private static Image getImage(int _image)
- {
- Image image = FONT_CHARS[_image];
-
- if (image == null)
- {
- image = new Image("datas/font" + _image + ".bmp");
- FONT_CHARS[_image] = image;
- }
-
- return image;
- }
-
- /**
- * Draws the text.
- * @param x The x coordinate
- * @param y The y coordinate
- * @param g The drawing area
- */
-
- public void drawText(int x, int y, Graphics g)
- {
- int c;
- int spaces = 0, lines = 0, width = 0;
-
- for (int i = 0; i < length; i++)
- {
- c = datas[i];
- g.drawImage(getImage(c), x + (width * FONT_WIDTH) + spaces, y + lines * (FONT_HEIGHT + 2));
-
- switch (mode)
- {
- case HORIZONTAL:
- spaces += 2;
- width++;
- break;
- case VERTICAL:
- lines++;
- break;
- }
- }
- }
-
- /**
- * Returns the width of the text.
- */
-
- public int getTextWidth()
- {
- switch (mode)
- {
- case HORIZONTAL:
- return length * (FONT_WIDTH + 2);
- case VERTICAL:
- return FONT_WIDTH;
- }
- return -1;
- }
-
- /**
- * Returns the height of the text.
- */
-
- public int getTextHeight()
- {
- switch (mode)
- {
- case VERTICAL:
- return length * (FONT_HEIGHT + 2);
- case HORIZONTAL:
- return FONT_HEIGHT;
- }
- return -1;
- }
- }
-
- // End of MiniString.java
-